package com.suxin.http.model;

import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Tang
 * @classname Query
 * @description [  ]
 * @date 2023/3/3 14:28
 */
public class Query {

    private final Map<String, Object> params;

    public final static Query EMPTY = newInstance();

    public Query() {
        this.params = new HashMap<>();
    }

    public static Query newInstance() {
        return new Query();
    }

    public Query addParam(String key, String value) {
        this.params.put(key, value);
        return this;
    }

    public Query build() {
        return this;
    }

    public Object get(String key) {
        return this.params.get(key);
    }

    public Query addAll(Map<String, Object> header) {
        this.params.putAll(header);
        return this;
    }

    public String toQueryUrl() {
        if (CollectionUtils.isEmpty(params)) {
            return "";
        }
        List<String> list = new ArrayList<>();
        for (String key : params.keySet()) {
            list.add(key + "=" + params.get(key));
        }
        return String.join(",", list.toArray(new String[]{}));
    }

}
